package com.slipper.core.poi.excel;

import com.slipper.exception.CommonException;
import com.slipper.util.FileUtil;

import java.io.*;
import java.util.*;

public class ExcelEngine implements Closeable {
    private final ExcelConfig config;
    private final ExcelTemplate template;

    public ExcelEngine(ExcelConfig config) throws IOException {
        this.config = config;
        this.template = new ExcelTemplate(getInputStream());
    }

    public void render(Map<String, Object> data) {
        this.template.render(data);
    }

    public void write(File file) throws IOException {
        this.template.write(file);
    }

    public void write(OutputStream outputStream) throws IOException {
        this.template.write(outputStream);
    }

    private InputStream getInputStream() {
        byte[] fileBytes = null;
        switch (config.getMode()) {
            case PATH -> fileBytes = FileUtil.readBytes(config.getPath());
            case FILE -> fileBytes = FileUtil.readBytes(config.getFile());
            case STREAM -> fileBytes = FileUtil.readBytes(config.getInputStream());
        }
        if (fileBytes == null) throw new CommonException("模板不能为空！");
        return new ByteArrayInputStream(fileBytes);
    }

    @Override
    public void close() throws IOException {
        if (this.template != null) {
            this.template.close();
        }
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("name", "模板名称");
        data.put("sex", "男");
        data.put("age", 40);
        data.put("日期", "2024-09-27");
        data.put("中文日期", "2024年9月27日");

        List<Map<String, Object>> dataList = new ArrayList<>() {{
            add(new HashMap<>() {{
                put("名称1", "name1");
                put("名称2", "name2");
                put("名称3", "name3");
                put("名称4", "name4");
                put("名称5", "name5");
                put("名称6", "name6");
                put("备注", "备注内容信息");
                put("description", "description");
                put("record", 10);
            }});
            add(new HashMap<>() {{
                put("名称1", "name1");
                put("名称2", "name21");
                put("名称3", "name31");
                put("名称4", "name41");
                put("名称5", "name51");
                put("名称6", "name61");
                put("备注", "备注内容信息1");
                put("description", "description1");
                put("record", 11);
            }});
            add(new HashMap<>() {{
                put("名称1", "name1");
                put("名称2", "name22");
                put("名称3", "name32");
                put("名称4", "name42");
                put("名称5", "name52");
                put("名称6", "name62");
                put("备注", "备注内容信息2");
                put("description", "description2");
                put("record", 12);
            }});
            add(new HashMap<>() {{
                put("名称1", "name13");
                put("名称2", "name23");
                put("名称3", "name33");
                put("名称4", "name43");
                put("名称5", "name53");
                put("名称6", "name63");
                put("备注", "备注内容信息3");
                put("description", "description3");
                put("record", 13.01);
            }});
        }};
        data.put("dataList", dataList);

        List<List<Integer>> mergeList = new ArrayList<>() {{
           add(Arrays.asList(0, 2, 0, 0));
           add(Arrays.asList(1, 1, 1, 2));
        }};
        data.put("merge_dataList", mergeList);

        String path = "C:\\Users\\joknm\\Desktop\\示例模板.xlsx";
        ExcelEngine engine = new ExcelEngine(new ExcelConfig(path));
        engine.render(data);
        engine.write(new File("C:\\Users\\joknm\\Desktop\\模板结果.xlsx"));
        engine.close();
    }
}
